import { apiSfctdPage } from '@/api/url/checkModule';
import router from '@/router';
import { isNullPm, isParam } from '@/utils/baseMethods';
import { reactive } from 'vue';

export default function importConfig() {
  const base = reactive({
    pagination: {
      defaultPageSize: 10,
      total: 100, //总条数
      current: 1,
      pageSize: 10,
      showTotal: true,
      showPageSize: true,
      pageSizeOptions: [10, 20, 30, 40, 50],
      hideOnSinglePage: true,
    },
    tabelList: null,
    checkFrom: {
      mv002: null,
      td001: 'D420',
      td002: null,
      td010: null,
      te006: null,
      te007: null,
      te017: null,
    },
    boderPn: 'D410',
  });

  const init = () => {
    getTable();
  };
  const getTable = () => {
    const steam: any = {
      pagesize: base.pagination.pageSize,
      page: base.pagination.current,
    };
    Object.keys(base.checkFrom).map((res: any) => {
      if (!isNullPm(base.checkFrom[res])) {
        steam[res] = base.checkFrom[res];
      }
    });
    steam.td001 = base.boderPn;
    apiSfctdPage(steam).then((res: any) => {
      if (res.code === 1) {
        base.tabelList = res.data.records;
        base.pagination.total = res.data.total;
      }
    });
  };

  const columns = [
    {
      title: '单别',
      dataIndex: 'td001',
      align: 'center',
    },
    {
      title: '单号',
      dataIndex: 'td002',
      align: 'center',
    },
    {
      title: '工作中心',
      dataIndex: 'md002',
      align: 'center',
    },
    {
      title: '生产日期',
      dataIndex: 'td003',
      align: 'center',
    },
    {
      title: '员工名称',
      dataIndex: 'mv002',
      align: 'center',
    },
    {
      title: '品号',
      dataIndex: 'te017',
      align: 'center',
    },
    {
      title: '工单单别',
      dataIndex: 'te006',
      align: 'center',
    },
    {
      title: '工单单号',
      dataIndex: 'te007',
      align: 'center',
    },
    {
      title: '工单批序',
      dataIndex: 'te003',
      align: 'center',
    },
    {
      title: '工序',
      dataIndex: 'te008',
      align: 'center',
    },
    {
      title: '工艺',
      dataIndex: 'te009',
      align: 'center',
    },
    {
      title: '工艺说明',
      dataIndex: 'mw003',
      align: 'center',
    },
    {
      title: '类型',
      dataIndex: 'te010',
      align: 'center',
      render: ({ record }) => {
        return TE010ALL(record.te010).label;
      },
    },
    // {
    //   title: '数量',
    //   dataIndex: 'te011',
    //   align: 'center'
    // },
    {
      title: '操作',
      slotName: 'optional',
      align: 'center',
    },
  ];

  const collection = reactive([
    { label: '公司名称', value: '', placeholder: '请输入公司名称', isInput: true },
    { label: '创建人', value: '', placeholder: '请输入公司名称', isSelect: true },
    { label: '供应商 ID', value: '', placeholder: '请输入公司名称', isRangePicker: true },
    { label: '供应商名称', value: '', placeholder: '请输入公司名称', isInput: true },
    { label: '负责人', value: '', placeholder: '请输入公司名称', isInput: true },
    { label: '负责人号码', value: '', placeholder: '请输入公司名称', isInput: true },
  ]);

  /**
   * @exports TD010ALL
   * 0.待处理、S.传送中、1.签核中、2.退件、3.已核准、4.撤销审核中、5.作废中、6.取消作废中、N.不运行电子签核
   */
  const TD010ALL = (param?: string | undefined) => {
    const steam: any = [
      {
        value: '0',
        label: '待处理',
      },
      {
        value: 's',
        label: '传送中',
      },
      {
        value: '1',
        label: '签核中',
      },
      {
        value: '2',
        label: '退件',
      },
      {
        value: '3',
        label: '已核准',
      },
      {
        value: '4',
        label: '撤销审核中',
      },
      {
        value: '5',
        label: '作废中',
      },
      {
        value: '6',
        label: '取消作废中',
      },
      {
        value: 'N',
        label: '不运行电子签核',
      },
    ];
    if (param) {
      return isParam(steam, 'value', param).value;
    } else {
      return steam;
    }
  };
  /**
   * @exports TE010ALL
   * 转移类型(1:正常完成 2:返工完成 3:报废 )
   */
  const TE010ALL = (param?: string | undefined) => {
    const steam: any = [
      {
        value: '1',
        label: '正常完成',
      },
      {
        value: '2',
        label: '返工完成',
      },
      {
        value: '3',
        label: '报废',
      },
    ];
    if (param) {
      return isParam(steam, 'value', param).value;
    } else {
      return steam;
    }
  };

  const handlePageChange = (page: number) => {
    base.pagination.current = page;
    init();
  };
  const handlePageSizeChange = (pageSize: number) => {
    base.pagination.pageSize = pageSize;
    init();
  };

  /**
   * @exports viewDetails 查看详情单
   * @param param 品号
   */
  const viewDetails = (param) => {
    // console.log(param)
    router.push({
      path: '/inspectionPolicy',
      query: { td001: param.td001.trim(), td002: param.td002.trim(), te003: param.te003.trim(), TYPENAME: '过程检验' },
    });
  };

  /**
   * @exports viewInspect 调起检验单
   */
  const viewInspect = () => {};

  /**
   *
   */
  const gotoPrint = () => {
    router.push({ name: 'inspectionPolicy', query: { TYPENAME: '过程检验' } });
  };
  return {
    columns,
    collection,
    handlePageChange,
    handlePageSizeChange,
    base,
    viewDetails,
    viewInspect,
    init,
    gotoPrint,
  };
}
